package com.example.controller;


import com.example.config.ResponseEntity;
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/getall")
    public ResponseEntity getUsers(){
        return ResponseEntity.success(userService.getUsers());
    }

    @RequestMapping("/getById/{id}")
    public ResponseEntity getUserById(@PathVariable("id") int id){
        return ResponseEntity.success(userService.getUserById(id));
    }

    @RequestMapping("/save")
    public ResponseEntity saveUser(@RequestBody User user){
        userService.saveUser(user);
        return ResponseEntity.success();
    }

    @RequestMapping("/delete/{id}")
    public ResponseEntity deleteUser(@PathVariable("id") int id){
        userService.deleteUser(id);
        return ResponseEntity.success();
    }
}
